﻿using System;
using System.Collections.Generic;
 
using System.Text;
using System.Net;

namespace GGSharp.Packets
{
    public class Status77Packet : HeaderPacket, IStatusReply
    {
        private int _uin;	        /* numer plus flagi w najstarszym bajcie */
        private byte _status;	        /* nowy stan */
        private IPAddress _remoteIP;		/* adres IP bezpośrednich połączeń */
        private short _remotePort;	/* port bezpośrednich połączeń */
        private byte _version;		/* wersja klienta */
        private byte _imageSize;	/* maksymalny rozmiar grafiki */
        private byte _unknown1;		/* 0x00 */
        private int _unknown2;		/* 0x00000000 */
        private string _description = "";	/* opis, nie musi wystąpić */
        private int _time = 0;		/* czas, nie musi wystąpić */

        protected bool _isAudioEnabled = false;
        protected bool _isAudio7Enabled = false;
        protected bool _isConnectedViaEraOmnix = false;

        public Status77Packet()
        {
            this.Type = PacketType.GG_STATUS77;
        }

        #region Properties

        public ClientVersion ClientVersion
        {
            get { return TranslateClientVersion(_version); }
        }

        public byte ClientVersionRaw
        {
            get { return _version; }
        }

        public string Description
        {
            get { return _description; }
        }

        public byte ImageSize
        {
            get { return _imageSize; }
        }

        public bool IsConnectedViaEraOmnix
        {
            get { return _isConnectedViaEraOmnix; }
        }

        public bool IsAudioEnabled
        {
            get { return _isAudioEnabled; }
        }

        public bool IsAudio7Enabled
        {
            get { return _isAudio7Enabled; }
        }

        public int Number
        {
            get { return _uin; }
        }

        public IPAddress RemoteIPAddress
        {
            get { return _remoteIP; }
        }

        public short RemotePort
        {
            get { return _remotePort; }
        }

        public GGShowType Show
        {
            get { return TranslateShow(_status); }
        }

        public byte ShowRaw
        {
            get { return _status; }
        }

        #endregion

        public override void Read(System.IO.BinaryReader reader)
        {
            base.Read(reader);

            int uin = reader.ReadInt32();
            _isConnectedViaEraOmnix = (uin & UINFLAG_ERA_OMNIX) > 0;
            _isAudioEnabled = (uin & UINFLAG_AUDIO) > 0;
            _isAudio7Enabled = (uin & UINFLAG_AUDIO7) > 0;
            _uin = uin & 0x00ffffff;
            _status = reader.ReadByte();
            _remoteIP = new IPAddress(reader.ReadUInt32());
            _remotePort = reader.ReadInt16();
            _version = reader.ReadByte();
            _imageSize = reader.ReadByte();
            _unknown1 = reader.ReadByte();
            _unknown2 = reader.ReadInt32();

            if (HasDescription(Show))
            {
                int descriptionSize = this.Length - 18;
                byte[] descriptionBytes = reader.ReadBytes(descriptionSize);


                _description = ReadCP1250Text(descriptionBytes);
                _time = GetTimeFromDescription(descriptionBytes, _description.Length);
            }
        }


    }
}
